package com.hjm.common.utils;

import com.hjm.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @ClassName BeanClassUtils
 * @Deacription 实体类工具
 * @Author Gd
 * @Version 1.0
 **/
public class BeanClassUtils<T> {

    private final  String defaultStr = "";
    private final  Date defaultDate = new Date();
    private final  BigDecimal defaultDecimal = new BigDecimal(0);
    private final  Timestamp defaultTimestamp=new Timestamp(new Date().getTime());
    //赋默认值
    public T setDefaultValue(T object) {
        try {
            Class clazz = object.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for(int i=0;i<fields.length;i++){
                Field field = fields[i];
                String fieldName = field.getName();
                Class fieldClass=field.getType();
                field.setAccessible(true); //设置访问权限
                if(isFieldValueNull(fieldName,object)){
                    if (fieldClass == Integer.class ) {
                        field.set(object, defaultDecimal.intValue());
                    }else if (fieldClass == Long.class) {
                        field.set(object, defaultDecimal.longValue());
                    }else if (fieldClass == Float.class) {
                        field.set(object, defaultDecimal.doubleValue());
                    }else if (fieldClass == BigDecimal.class) {
                        field.set(object, defaultDecimal);
                    } else if (fieldClass == Date.class) {
                        field.set(object, defaultDate);
                    } else if (fieldClass == String.class){
                        field.set(object, defaultStr); // 设置值
                    } else if (fieldClass == Timestamp.class){
                        field.set(object, defaultTimestamp);
                    }
                }/*else if(isStringFieldValueNull(fieldName,object,fieldClass)){//MySQL，需要对对主键做特殊处理
                    field.set(object, null);
                }*/
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("转换属性失败");
        }
        return object;
    }

    //判断字段是否为空
    private  boolean  isFieldValueNull(String fieldName, Object object) throws ClassNotFoundException {
        boolean isNUll=false;
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(object, new Object[] {});
            if(value==null){
                isNUll=true;
            }
            return isNUll;
        } catch (Exception e) {
            return isNUll;
        }
    }

    //判断主键是否为空值
    private  boolean isStringFieldValueNull(String fieldName, Object object, Class fieldClass) throws ClassNotFoundException {
        boolean isNUll=false;
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(object, new Object[] {});
            if(value==null ){
                isNUll=true;
            }else{
                if (fieldClass == String.class && StringUtils.isBlank((String)value)) {
                    isNUll=true;
                }
            }
            return isNUll;
        } catch (Exception e) {
            return isNUll;
        }
    }


}
